package com.consul.config;

import com.consul.listener.MyElasticJobListener;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public abstract class BaseConfig {
    /**
     * 配置任务监听器
     *
     * @return
     */
    @Bean
    public ElasticJobListener elasticJobListener() {
        return new MyElasticJobListener();
    }

    /**
     * 轨迹配置
     *
     * @param url
     * @param username
     * @param password
     * @param driverClassName
     * @return
     */
    @Bean()
    public JobEventConfiguration jobEventConfiguration(@Value("${mysql.url}") String url,
                                                       @Value("${mysql.username}") String username,
                                                       @Value("${mysql.password}") String password,
                                                       @Value("${mysql.driver-class-name}") String driverClassName) {
        DataSource dataSource = DataSourceBuilder.create().driverClassName(driverClassName).url(url).username(username).password(password).build();
        return new JobEventRdbConfiguration(dataSource);
    }
}
